package thread_ex;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import java.awt.event.MouseMotionAdapter;

public class BallBounce extends JFrame {

	private JPanel contentPane;
	private JPanel panel;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BallBounce frame = new BallBounce();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public BallBounce() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 655, 502);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		contentPane.add(getPanel(), BorderLayout.CENTER);
	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setBackground(Color.WHITE);
			
			panel.addMouseMotionListener(new MouseMotionAdapter() {				
				@Override
				public void mouseMoved(MouseEvent pe) {
					int MouX = pe.getX();
					Box bo = new Box(panel,MouX);
					bo.start();
					
				}
			});
						
			panel.addMouseListener(new MouseAdapter() {				
				@Override
				public void mousePressed(MouseEvent e) {
					Point p = e.getPoint();
					Ball b = new Ball(panel,p);
					b.start();
				}
			});
		}
		return panel;
	}
}
